package com.clt.web.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.clt.web.common.WeChat;
import com.clt.web.common.WeiXinPropertiesUtil;
import com.clt.web.service.impl.WxCoreServiceImpl;


/**
 * 微信验证请求controller
 * @author wenjian
 *
 */
@Controller
@RequestMapping("/weixinCore")
public class WeixinCoreController {

	private static final Logger logger = Logger
			.getLogger(WeixinCoreController.class);

	@Autowired
	private WxCoreServiceImpl weixinCoreService;

	@RequestMapping("/coreProcess")
	public void coreProcess(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		String method = req.getMethod();
		if (method.equals("GET")) {
			coreGet(req, resp);
		} else {
			corePost(req, resp);
		}
	}

	/**
	 * 请求校验(确认请求来自微信服务器)
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @throws IOException
	 */
	private void coreGet(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String outPut = "error";
		String signature = request.getParameter("signature");// 微信加密签名
		String timestamp = request.getParameter("timestamp");// 时间戳
		String nonce = request.getParameter("nonce");// 随机数
		String echostr = request.getParameter("echostr");//随机字符串
		logger.info("signature="+signature+";timestamp="+timestamp+";nonce="+nonce+";echostr"+echostr);
		// 验证
		if (StringUtils.isNotBlank(signature)
				&& StringUtils.isNotBlank(timestamp)
				&& StringUtils.isNotBlank(nonce)
				&& WeChat.checkSignature(WeiXinPropertiesUtil.getToken(),
						signature, timestamp, nonce)) {
			outPut = echostr;
		}
		logger.error("微信验证结果"+outPut);
		response.getWriter().write(outPut);
	}

	/**
	 * 微信请求接口
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void corePost(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String respXml = null;
		String signature = request.getParameter("signature");// 微信加密签名
		String timestamp = request.getParameter("timestamp");// 时间戳
		String nonce = request.getParameter("nonce");// 随机数
		PrintWriter out = response.getWriter();
		// 验证
		if (StringUtils.isNotBlank(signature)
				&& StringUtils.isNotBlank(timestamp)
				&& StringUtils.isNotBlank(nonce)
				&& WeChat.checkSignature(WeiXinPropertiesUtil.getToken(),
						signature, timestamp, nonce)) {
			respXml = weixinCoreService.processRequest(request);
		} else {
			logger.error("微信不知名的接入，验证失败");
		}
		out.print(respXml);
		out.close();
		out = null;
	}

}
